package com.kaishengit.pms.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.pms.entity.Employee;
import com.kaishengit.pms.service.EmployeeService;
import com.kaishengit.pms.util.PKUtil;

public class EmployeeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

		protected void service (HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("m");
		
		if(method == null) {
			response.sendError(405,"��������");
		} else if("show".equals(method)) {
			//��ʾ���й�Ա
			showAllEmployees(request,response);
		} else if("add".equals(method)) {
			//������ת����ӹ�Աҳ��
			request.getRequestDispatcher("WEB-INF/views/addemployee.jsp").forward(request, response);
		}else if("addemployee".equals(method)){
			//��ӹ�Ա
			addEmployee(request, response);
		} else if("modify".equals(method)){
			//��������
			resetPwd(request,response);
		} else if("del".equals(method)) {
			//ɾ���û�
			delEmployee(request,response);
		}
	}

		/**
		 * ɾ���Ա
		 * @param request
		 * @param response
		 * @throws IOException 
		 */
	private void delEmployee(HttpServletRequest request,
				HttpServletResponse response) throws IOException {
			String id = request.getParameter("id");
			
			EmployeeService ems = new EmployeeService();
			ems.delEmployee(id);
			response.sendRedirect("employee.jspx?m=show");
		}

	/**
	 * ��������
	 * @param request
	 * @param response
	 */
	private void resetPwd(HttpServletRequest request,
				HttpServletResponse response) {
			String id = request.getParameter("id");
			String password = request.getParameter("password");
			EmployeeService ems = new EmployeeService();
			
			try {
				
				ems.resetPwd(id,password);
				//�޸ĳɹ�����ת���û��б�
				response.sendRedirect("employee.jspx?m=show");
			} catch (Exception e) {
				//�޸Ĳ��ɹ�,��ȡ������Ϣ
				e.getMessage();
			}
		}

	//��ӹ�Ա
	private void addEmployee(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String id = PKUtil.getId();
		String cookiepwd = PKUtil.getId();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		
		EmployeeService em = new EmployeeService();
		em.addEmployee(id,cookiepwd,username,password);
		
		response.sendRedirect("employee.jspx?m=show");
	}

	//��ʾ���й�Ա��Ϣ
	private void showAllEmployees(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		
		EmployeeService ems = new EmployeeService();
		List<Employee> employees = ems.findAllEmployees();
		
		//�ѹ�Ա���Ϸ���request������
		request.setAttribute("employees", employees);
		//��ת����ʾҳ��
		request.getRequestDispatcher("WEB-INF/views/showemployee.jsp").forward(request, response);
	}

}
